package com.lg.atp.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.lg.atp.common.Constants;
import com.lg.atp.entity.TblAppInfo;
import com.lg.atp.service.IAppInfoService;

@RestController
@RequestMapping("app")
public class AppListController {

	private static final Log log = LogFactory.getLog(AppListController.class);
	
	@Autowired
	private IAppInfoService iAppInfoService;
	
	@GetMapping("index")
	public ModelAndView toIndex() {
		ModelAndView view = new ModelAndView();
		view.setViewName("appList");
		return view;
	}
	
	@GetMapping("toAdd")
	public ModelAndView toAdd() {
		ModelAndView view = new ModelAndView();
		view.setViewName("app-add");
		return view;
	}
	
	
	@GetMapping("toEdit")
	public ModelAndView toEdit(Long id) {
		ModelAndView view = new ModelAndView();
		view.setViewName("app-edit");
		view.addObject("appId",id);
		return view;
	}
	
	@PostMapping("list")
	public Map<String,Object> selectAppWithPages(String appName,String appNumber,Integer editStatus,Integer pageIndex, Integer pageSize){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			TblAppInfo app = new TblAppInfo();
			app.setAppName(appName);
			app.setAppNumber(appNumber);
			app.setEditStatus(editStatus);
			PageInfo<TblAppInfo> list = iAppInfoService.selectAllApp(app, pageIndex, pageSize);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取应用列表失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取应用列表失败");
		}
		return result;
	}

	@PostMapping("getApp")
	public Map<String,Object> getApp(Long id){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			TblAppInfo app = iAppInfoService.selectAppById(id);
			result.put("data", app);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取应用失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取应用失败");
		}
		
		return result;
	}

	@PostMapping("saveApp")
	public Map<String,Object> saveApp(TblAppInfo app){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try{
			Long id = app.getId();
			if(id == null)
				iAppInfoService.saveApp(app);
			else
				iAppInfoService.updateApp(app);
		}catch(Exception e){
			e.printStackTrace();
			log.error("保存应用失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "保存应用失败");
		}
		return result;
	}
	
	
	@PostMapping("deleteApp")
	public Map<String,Object> deleteApp(String ids){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try{
			iAppInfoService.deleteApps(ids);
		}catch(Exception e){
			e.printStackTrace();
			log.error("删除应用失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "删除应用失败");
		}
		return result;
		
	}

}
